package simple.zombie;

// JFC
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;

// GTGE
import com.golden.gamedev.*;
import com.golden.gamedev.object.*;

import java.awt.event.KeyEvent;

public class MainMenu {
	private static final String NEW_GAME 	= "NEW GAME";
	private static final String RESUME 		= "RESUME";
	private static final String CREDIT 		= "CREDIT";
	private static final String EXIT 			= "EXIT";
	private static final def newMenuContent 	= [ NEW_GAME, CREDIT, EXIT ];
	private static final def contMenuContent	= [ NEW_GAME, RESUME, CREDIT, EXIT ];
	private static final def creditContent = [ "THIS IS A TEST PROJECT.", "CREATED BY ME.", "POWERED BY GTGE." ];
	private static final def exitConfirmContent = [ "ARE YOU SURE?", "Y:N?" ];
	private static final def menuCoordinate = [ X: 200, Y: 200 ];
		
	private static final Boolean SHOW 				= true;
	private static final Boolean HIDE 				= false;
	private static final Boolean CONTINUE 		= true;
	private static final Boolean NOTCONTINUE	= false;
	
	private static def selectorInfo 	        = [ X: menuCoordinate.X - 2, Y: menuCoordinate.Y - 2, W: 130, H: 20 ];
	private static def pointerCoordinate 	    = [ X: 0, Y: 0 ];
	
	private Boolean showMenu					        = SHOW;
	private Boolean showContinue			        = NOTCONTINUE;
	private Boolean showCredit				        = HIDE;
	private int cursorPosition 				        = 0;
  private Boolean pointerIntersect          = false;
  
  public void callItem( index ) {
    switch( index ) {
      case 0: 
        callNewGame();
        break;
      case 1:
        if( showContinue ) {
          callResume();
        } else {
          callCredit();
        }
        break;
      case 2:
        if( showContinue ) {
          callCredit();
        } else {
          callExit();
        }
        break;
      case 3:
        if( showContinue ) {
          callExit();
        }
        break;
      default: break;
    }
  }
	
	public def callNewGame() {
		switchMenuStatus();
    if( !showContinue ) {
      switchContinue();
    }
	}
	
	public def callResume() {
		switchMenuStatus();
	}
	
	public def callCredit() {
		switchShowCredit();
	}
	
	public def callExit() {
		System.exit( 0 );
	}
  
  public def getPointerCoordinate() {
    return pointerCoordinate;
  }
  
  public void setPointerCoordinate( x, y ) {
    pointerCoordinate.X = x;
    pointerCoordinate.Y = y;
  }
  
  public pointerClick( x, y ) {
    setPointerCoordinate( x, y );
    if( showContinue ) {
      contMenuContent.eachWithIndex() { item, index ->
        if( checkPointerIntersect(
          pointerCoordinate.X, pointerCoordinate.Y, 
          menuCoordinate.X, index * 25 + menuCoordinate.Y - 2, 
          selectorInfo.W, selectorInfo.H ) ) {
          if( cursorPosition != index ) {
            setCursorPosition( index );
            moveSelector();
          } else {
            callItem( index );
          }
        }
      }
    } else {
      newMenuContent.eachWithIndex() { item, index ->
        if( checkPointerIntersect(
          pointerCoordinate.X, pointerCoordinate.Y, 
          menuCoordinate.X, index * 25 + menuCoordinate.Y - 2, 
          selectorInfo.W, selectorInfo.H ) ) {
          if( cursorPosition != index ) {
            setCursorPosition( index );
            moveSelector();
          } else {
            callItem( index );
          }
        }
      }
    }
  }
  
  public boolean checkPointerIntersect( pointerX, pointerY, rectX, rectY, rectW, rectH ) {
    println "Checking intersect:";
    println "PointerX: $pointerX, pointerY: $pointerY, rectX: $rectX, rectY: $rectY, rectW: $rectW, rectH: $rectH";
    if( pointerX >= rectX && pointerX <= rectX + rectW && pointerY >= rectY && pointerY <= rectY + rectH ) {
      return true;
    }
    return false;
  }
	
	public int getCursorPosition() {
		return cursorPosition;
	}
	
	public def setCursorPosition( newPosition ) {
    println "Cursor old pos: $cursorPosition";
		cursorPosition = newPosition;
    println "Cursor new pos: $cursorPosition";
	}
	
	public String getSelectedMenuItem() {
		if( showContinue ) {
			return contMenuContent[ getCursorPosition() ];
		} else {
			return newMenuContent[ getCursorPosition() ];
		}
		
	}
	
	public def getMenuContent() {
		if( showContinue ) {
			return contMenuContent;
		} else {
			return newMenuContent;
		}
	}
	
	public def getCreditContent() {
		return creditContent;
	}
	
	public def menuDown() {
		cursorPosition++;
		if( showContinue && cursorPosition > 3 ) {
			cursorPosition = 0;
		}
		if( !showContinue && cursorPosition > 2 ) {
			cursorPosition = 0;
		}
		moveSelector();
	}
	
	public def menuUp() {
		cursorPosition--;
		if( cursorPosition < 0 ) {
			if( showContinue ) {
				cursorPosition = 3;
			} else {
				cursorPosition = 2;
			}
		}
		moveSelector();
	}
  
  public def moveSelector() {
		selectorInfo.Y = cursorPosition * 25 + menuCoordinate.Y - 2;
  }
	
	public def switchMenuStatus() {
		showMenu = !showMenu;
		if( showMenu && showCredit ) {
			switchShowCredit();
		}
	}
	
	public def switchContinue() {
		showContinue = !showContinue;
	}
	
	public def switchShowCredit() {
		showCredit = !showCredit;
		if( showCredit && showMenu ) {
			switchMenuStatus();
		}
	}
	
	public Boolean isShowMenu() {
		return showMenu;
	}
	
	public Boolean isShowCredit() {
		return showCredit;
	}
  
  public void callAction() {
    callItem( getCursorPosition() );
    // if( getSelectedMenuItem() == EXIT ) {
      // callExit();
    // }
    // if( getSelectedMenuItem() == NEW_GAME ) {
      // callNewGame();
    // }
    // if( getSelectedMenuItem() == RESUME ) {
      // callResume();
    // }
    // if( getSelectedMenuItem() == CREDIT ) {
      // callCredit();
    // }
  }
}